<?php

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

DB::connection()->disableQueryLog(); 


// $duiying=array(
// 	44	=>array('accelerator_pedal'),
// 	48	=>array('air_conditioning_filter'),
// 	52	=>array('brake_pedal'	),
// 	56	=>array('clutch_pedal'	),
// 	61	=>array('engine_computer'	),
// 	67	=>array('horn_sound','wiper_blade_angle','wiper_blade_clean'	),
// 	72	=>array('wiper_water_spray'	),
// 	77	=>array('after_damper'	),
// 	81	=>array('bearing'	),
// 	87	=>array('chassis_screw'	),
// 	93	=>array('dust_cover'	),
// 	98	=>array('escape_pipe'	),
// 	103	=>array('front_damper'	),
// 	108	=>array('spare_tire','speed_changing_box'	),
// 	120	=>array('air_cleaner'	),
// 	124	=>array('air_inlet_line'	),
// 	130	=>array('bath_of_glass','belt','cornering_oil','damping_oil','engine_oil','fattery_power','freezing_level','freezing_line','freezing_point','spark_plug','left_after_age_limit','left_after_brake_block','left_after_brake_disk','left_after_facade','left_after_flank_wear','left_after_tire_pressure','left_after_tread','left_after_valve_cock','left_front_age_limit','left_front_brake_block','left_front_brake_disk','left_front_facade','left_front_flank_wear','left_front_tire_pressure','left_front_tread','left_front_valve_cock','right_after_age_limit'	),
// 	135	=>array('right_after_brake_block','right_after_brake_disk','right_after_facade'	),
// 	136	=>array('right_after_valve_cock'	),
// 	142	=>array('right_front_age_limit','right_front_brake_block'	),
// 	145	=>array('right_front_brake_disk'	),
// 	148	=>array('right_front_facade'	),
// 	153	=>array('right_front_flank_wear','right_front_tire_pressure'	),
// 	154	=>array('right_front_tread','right_front_valve_cock','after_cornering_lamp'	),
// 	158	=>array('after_fog_lamp','after_hazard_warning_light'	),
// 	162	=>array('after_wide_lamp'	),
// 	165	=>array('back_up_lamp'	),
// 	168	=>array('dipped_headlight'	),
// 	171	=>array('distance_headlight','front_cornering_lamp'	),
// 	172	=>array('front_fog_lamp'	),
// 	173	=>array('front_hazard_warning_light'	),
// 	174	=>array('front_wide_lamp'	),
// 	175	=>array('hazard_warning_light'	),
// 	176	=>array('stop_lamp'	),

// );
$duiying=array(
    44  =>array('engine_oil'),
    48  =>array('air_cleaner'),
    52  =>array('air_conditioning_filter'),
    56  =>array('damping_oil'),
    61  =>array('cornering_oil'),
    67  =>array('freezing_level','freezing_line','freezing_point'),
    72  =>array('fattery_power'),
    77  =>array('bath_of_glass'),
    81  =>array('air_inlet_line','air_inlet_line'),
    87  =>array('spark_plug'),
    93  =>array('belt'),
    98  =>array('speed_changing_box'),
    103 =>array('dust_cover','dust_cover','after_damper','front_damper'),
    120 =>array('bearing'),
    124 =>array('escape_pipe','escape_pipe'),
    130 =>array('spare_tire','left_after_age_limit','left_after_facade','left_after_flank_wear','left_after_tire_pressure','left_after_tread','left_after_valve_cock','left_front_age_limit','left_front_facade','left_front_flank_wear','left_front_tire_pressure','left_front_tread','left_front_valve_cock','right_after_age_limit','right_after_facade','right_after_flank_wear','right_after_tire_pressure','right_after_tread','right_after_valve_cock','right_front_age_limit','right_front_facade','right_front_flank_wear','right_front_tire_pressure','right_front_tread','right_front_valve_cock'),
    135 =>array('left_after_brake_block','left_after_brake_disk','left_front_brake_block','left_front_brake_disk','right_front_brake_block','right_front_brake_disk','right_after_brake_block','right_after_brake_disk'),
    136 =>array('chassis_screw'),
    142 =>array('after_wide_lamp','front_wide_lamp'),
    145 =>array('dipped_headlight'),
    148 =>array('distance_headlight'),
    153 =>array('after_cornering_lamp','front_cornering_lamp'),
    154 =>array('after_hazard_warning_light','front_hazard_warning_light','hazard_warning_light'),
    158 =>array('after_fog_lamp','front_fog_lamp'),
    162 =>array('stop_lamp'),
    165 =>array('back_up_lamp'),
    168 =>array('wiper_water_spray'),
    171 =>array('wiper_blade_angle','wiper_blade_clean','wiper_blade_clean'),
    172 =>array('engine_computer'),
    173 =>array('horn_sound'),
    174 =>array('accelerator_pedal'),
    175 =>array('brake_pedal'),
    176 =>array('clutch_pedal'),
);
//找出所有的 有检测记录的订单 
// $all_order=DB::select("SELECT DISTINCT order_id FROM order_30_check");
$all_order=DB::select("SELECT DISTINCT order_id FROM order_30_check where  order_id in(14651962721211)");
foreach ($all_order as $_o) {//循环构建 这些订单的 检测报告
	$order=Orders::find($_o->order_id);
    if(is_null($order)){
        continue;
    }
    $t_o30c=Order30Check::where("order_id",$order->order_id)->first();
	$de=DetectionTemplate::where("level",1)->get();
	OrderDetection::where("order_id",$order->order_id)->delete();
    foreach ($de as $_de) {
        $one_od=new OrderDetection;
    	$one_od->order_id=$order->order_id;
    	$one_od->name=$_de->name;
    	$one_od->c_name=$_de->c_name;
    	$one_od->parent_id=0;
    	$one_od->dt_id=$_de->id;
    	$one_od->level=1;
    	$one_od->c_desc=$_de->c_desc;
    	$one_od->fraction=$_de->fraction;
    	$one_od->status=0;
    	$one_od->sort=$_de->sort;
    	$one_od->create_by=$order->engineer_id;
        $one_od->created_at=$t_o30c->created_at;
    	$one_od->save();
        $two_de=DetectionTemplate::where("parent_id",$_de->id)->where("level",2)->get();
        foreach ($two_de as $_t_de) {
            $two_od=new OrderDetection;
	    	$two_od->order_id=$order->order_id;
	    	$two_od->name=$_t_de->name;
	    	$two_od->c_name=$_t_de->c_name;
	    	$two_od->parent_id=$one_od->id;
	    	$two_od->dt_id=$_t_de->id;
	    	$two_od->level=2;
	    	$two_od->c_desc=$_t_de->c_desc;
	    	$two_od->fraction=$_t_de->fraction;
	    	$two_od->status=0;
	    	$two_od->sort=$_t_de->sort;
	    	$two_od->create_by=$order->engineer_id;
            $two_od->created_at=$t_o30c->created_at;
	    	$two_od->save();
            $temp_de=DetectionTemplate::where("parent_id",$_t_de->id)->where("level",3)->get();
            foreach ($temp_de as $_three_temp_de) {
            	//查看元检测记录 该项对应的检测结果是否一异常
            	$status=0;
            	if(isset($duiying[$_three_temp_de->id])){//说明该项对应的有旧的数据 
            		$o30c=Order30Check::where("order_id",$order->order_id)->whereIn("item_id",$duiying[$_three_temp_de->id])->where("status",1)->first();
            		if($o30c){//说明该新数据对应的旧数据 有异常项
            			$status=1;
            		}
            	}           	
                $three_od=new OrderDetection;
    	    	$three_od->order_id=$order->order_id;
    	    	$three_od->name=$_three_temp_de->name;
    	    	$three_od->c_name=$_three_temp_de->c_name;
    	    	$three_od->parent_id=$two_od->id;
    	    	$three_od->dt_id=$_three_temp_de->id;
    	    	$three_od->level=3;
    	    	$three_od->c_desc=$_three_temp_de->c_desc;
    	    	$three_od->fraction=$_three_temp_de->fraction;
    	    	$three_od->status=$status;
    	    	$three_od->sort=$_three_temp_de->sort;
    	    	$three_od->create_by=$order->engineer_id;
                $three_od->created_at=$t_o30c->created_at;
    	    	$three_od->save();
    	    	if($status==1){//如果异常 则把该对应的 2级1级 改成异常
    	    		$one_od=OrderDetection::find($one_od->id);
    	    		$one_od->status=1;
			    	$one_od->save();
			    	$two_od=OrderDetection::find($two_od->id);
    	    		$two_od->status=1;
			    	$two_od->save();
    	    	}
            }
        }
    }  

    $o30c=Order30Check::where("order_id",$order->order_id)->where("item_id","suggest")->first();
    OrderDetectionDesc::where("order_id",$order->order_id)->delete();
    $odc=new OrderDetectionDesc;
    $odc->order_id=$order->order_id;
    $odc->note=isset($o30c)?$o30c->value:'';
    $odc->auth_by=0;
    $odc->create_by=$order->engineer_id;
    $odc->created_at=$t_o30c->created_at;
    $odc->save();
    Log::info("订单".$order->order_id.' 构建新的检测报告完毕');
	echo "订单".$order->order_id.' 构建新的检测报告完毕';
}
?>